#pragma once
#include<iostream>
using namespace std;
namespace GIS20172
{
	int  Hash(int key)
	{
		return key % 9;
	}

	int arr[] = { 23,45,35,35,52,68,11,7,67,65 };
	int size = 10;
	struct backetnode
	{
		int key;
		backetnode* next = nullptr;
	};

	class Hashbacket
	{
	public:
		backetnode* arr[9] = { nullptr };

		void hashadd(int key)
		{
			int hashkey = Hash(key);
			backetnode* newnode = (backetnode*)malloc(sizeof(backetnode));
			newnode->key = key;
			if (arr[hashkey] == nullptr)
				arr[hashkey] = newnode;
			else
			{
				backetnode* cur = arr[hashkey];
				while (cur->next == nullptr)
				{
					cur = cur->next;
				}
				cur->next = newnode;
			}
		}

		bool hashpop(int key)
		{
			int hashkey = Hash(key);
			backetnode* cur = arr[hashkey];
			backetnode* pre = nullptr;
			while (cur != nullptr)
			{
				if (cur->key == key)
				{
					backetnode* nextnode = cur->next;
					free(cur);
					pre->next = nextnode;
					return true;
				}
				pre = cur;
				cur = cur->next;
			}
			return false;
		}
	};

	void function()
	{
		Hashbacket hb;
		for(int i=0;i<size;i++)
		{
			hb.hashadd(arr[i]);
		}

		for (int i = 0; i < 9; i++)
		{
			if (hb.arr[i] != nullptr)
			{
				backetnode* cur = hb.arr[i];
				while (cur != nullptr)
				{
					cout << cur->key << ' ';
					cur = cur->next;
				}
			}
		}
	}
}